﻿
using DataWorks_Sqlite.MappingClass;
using DataWorks_Tools.BasicMethods;
using DataWorks_Tools.HalfHourCalSave.Common.Basic;
using DataWorks_Tools.MappModals.CalInputModals;
using DataWorks_Tools.MappModals.CalSaveModals;
using RainFlowandDamageTool.ComputingProcess;
using Yitter.IdGenerator;
using static DataWorks_Tools.BasicMethods.ReadfromCSV;

namespace DataWorks_Tools.HalfHourCalSave.Common.DataImport
{
    public static class Tools_SteeringImport
    {
        /// <summary>
        /// 已弃用
        /// </summary>
        /// <param name="csvimport"></param>
        /// <param name="admin"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        [Obsolete]
        public static async Task<List<calsave_steering_split>> GetSteeringAsync(CSVImport csvimport, dw_props_admin admin, HalfHourCalInputModel user)
        {
            var t = await CommonMehtods.GetConditonDelAsync(csvimport, admin, user,
                () => { return user.chantitle_steering.ReName(); },
                () => { return user.chantitle_accybody.ReName(); },
                () => { return admin.steeringzerostandard; },
                (s, t) => { return s <= t; },
                (a, b, c, d, e) =>
                {
                    calsave_steering_split record = new calsave_steering_split();
                    record.steeringacc = Math.Abs(a.Max()) > Math.Abs(a.Min()) ? a.Max() : a.Min();
                    record.wheelangle = b.Max();
                    record.spd = c.Max();
                    record.project = user.project;
                    record.vehicle = user.vehicle;
                    
                    record.lastingtime = e - d;
                    record.datadate = Convert.ToDateTime(csvimport.datetimesecond);
                    record.filename = csvimport.name;
                    record.direction = (sbyte)(record.wheelangle > 0 ? 1 : -1);
                    record.subdirectory = csvimport.subdirectory;
                    return record;
                });
            return t;
        }
        public static async Task<List<calsave_steering_split>> GetSteeringV2Async(CSVImport csvimport, dw_props_admin admin, HalfHourCalInputModel props)
        {
            List<calsave_steering_split> result = new();
            if (props.importsteering == 1)
            {
                var spdlist = await ReadfromCSV.GetSpecifiedColumnDataAsync(csvimport, props.chantitle_spd);
                var wheelangle = await ReadfromCSV.GetSpecifiedColumnDataAsync(csvimport, props.chantitle_steering);
                var accyst= await ReadfromCSV.GetSpecifiedColumnDataAsync(csvimport, props.chantitle_accybody);
                if (spdlist != null && wheelangle!=null&&accyst!=null)
                {
                    var downfactor = admin.samplerate / admin.spdfromtoresamplerate;
                    //先降采样，降低到10hz，好处理
                    var downspdlist = spdlist.DownSample(downfactor);
                    var downwheelangle = wheelangle.DownSample(downfactor);
                    var downaccyst = accyst.DownSample(downfactor);
                    //先清零
                    var newwhlang= downwheelangle.DoSteeringZero(admin.steeringzerostandard);
                    if (newwhlang!=null)
                    {
                        //查找0-0的index

                        var index = newwhlang.FindSteeringZeroIndex();
                        if (index?.Count > 1)
                        {
                            int d = 1;//用于给filename区分每个fromto
                            for (int i = 0; i < index.Count - 1; i+=2)
                            {
                               
                                var lastingtime = (index[i + 1] - index[i])*(downfactor);//换算成512Hz的点数
                                var angle = downwheelangle.GetDataValues(index[i], index[i + 1]);//先获取当前0-0之间的转角数据
                                var whlangmax = downwheelangle.GetDataValues(index[i], index[i + 1]).Max();
                                var whlangmin = downwheelangle.GetDataValues(index[i], index[i + 1]).Min();
                                var whlang = Math.Abs(whlangmax) > Math.Abs(whlangmin) ? whlangmax : whlangmin;
                                var maxangidx = angle.IndexOf(whlang);//获取最大转角出现的索引号
                                if (maxangidx == -1)
                                {
                                    return result;
                                }
                                var spd = downspdlist.GetDataValues(index[i], index[i + 1])[maxangidx];
                                var spdbin = spd.GetSpdBin();
                                var direction = whlang > 0 ? 0 : 1;
                                var accy = downaccyst.GetDataValues(index[i], index[i + 1])[maxangidx];
                                var createTime = Convert.ToDateTime(csvimport.datetime);
                                for (int j = 1; j < csvimport.columncount; j++)
                                {
                                    calsave_steering_split row = new();
                                    row.key = YitIdHelper.NextId();
                                    row.createTime = createTime;
                                    row.vehicle = props?.vehicle;
                                    row.project = props?.project;
                                    row.filename = csvimport.name + "-steering-" + d;//区分每个fromto的名称
                                    row.datadate = Convert.ToDateTime(csvimport.datetimesecond);
                                    row.subdirectory = csvimport.subdirectory;
                                    row.spd = spd;
                                    row.spdbin = spdbin;
                                    row.lastingtime = lastingtime;
                                    row.wheelangle = whlang;
                                    row.steeringacc = accy;
                                    row.direction = (sbyte)direction;
                                    row.chantitle = csvimport.headers[j];
                                    var data = csvimport.lists[j].DownSample(downfactor).GetDataValues(index[i], index[i + 1]);
                                    row.smax = data.Max();
                                    row.smin = data.Min();
                                    row.smean = data.Average();
                                    row.srange = Math.Abs(row.smax - row.smin);
                                    double variance = data.Sum(x => Math.Pow(x - row.smean, 2)) / (data.Count - 1);
                                    row.std = Math.Sqrt(variance);
                                    row.damagek3 = data.GetAccumDamagefromList(3, 3, admin.binnumber);
                                    row.damagek5 = data.GetAccumDamagefromList(5, 5, admin.binnumber);
                                    double sumOfSquares = data.Sum(x => x * x);
                                    double meanSquare = sumOfSquares / (data.Count - 1);
                                    row.srms = Math.Sqrt(meanSquare);
                                    if (j == 1 || j == 2)
                                    {
                                        row.smax = data[0];
                                        row.smin = data[0];
                                        row.smean = data[0];
                                        row.srange = data[0];
                                        row.std = data[0];
                                        row.damagek3 = 0;
                                        row.damagek5 = 0;
                                        row.srms = data[0];
                                    }
                                    result.Add(row);
                                }
                                d++;
                            }
                        }
                    }
                }
            }
            return result;
        }
    }
}
